package com.zx._07_数据库.测试数据;

import cn.hutool.core.io.FileUtil;
import com.zx._07_数据库.jdbc.JDBC;
import java.sql.Connection;
import java.sql.Statement;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Table {

    public static boolean createTable() {
        String createTableSql = getCreateTableSql();

        Connection connection = JDBC.getConnection();
        log.info("createTable begin");
        Statement sta = null;

        try {
            sta = connection.createStatement();
            sta.execute(createTableSql);
            log.info("createTable finsh");
        } catch (Exception e) {
            log.error("createTable error", e);
            return false;
        } finally {
            JDBC.release(connection, sta, null);
        }
        return true;
    }

    private static String getCreateTableSql() {
        String path = Table.class.getClassLoader().getResource("").getPath();
        String filePath = path + "sql/init.sql";

        String sql = FileUtil.readString(filePath, "utf-8");

        log.info("createTable get sql, \r\n {}", sql);
        return sql;
    }
}
